<?php 
if (!isset($instance['category'])){
	$instance['category'] = 0;
}
extract($instance);
global $wpdb;
global $post;
 do_action( 'before' ); 
 if ( (in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || (in_array( 'jigoshop/jigoshop.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ))) ) { 
if($category != 0)
{
 $default = array(
	'post_type' => 'product',
	'tax_query'	=> array(
	array(
		'taxonomy'	=> 'product_cat',
		'field'		=> 'id',
		'terms'		=> $category)),
	'orderby' => $orderby,
	'order' => $order,
	'post_status' => 'publish',
	'showposts' => $numberposts
);
}
else{
 $default = array(
	'post_type' => 'product',
	'orderby' => $orderby,
	'order' => $order,
	'post_status' => 'publish',
	'showposts' => $numberposts
);
}


$loop = new WP_Query($default);
if ($loop->have_posts()){
?>
<div class="widget-above">
	<div class="above-content clearfix">
		<div class="row">
		<?php while ( $loop->have_posts() ) : $loop->the_post(); global $product, $post, $wpdb, $average; 
		global $product;
		//$price = get_post_meta($post->ID, '_regular_price', true);
		$cats = get_the_terms($post->ID, 'product_cat' );
		$thumb_image = get_the_post_thumbnail($post->ID, 'medium');
		?>
		
		<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), '' ); ?>
		<div class="col-lg-3 col-md-3 col-sm-6">
				<div class="item_product">
					<div class="item_product_in">
						<div class="title-cate">
							<?php if($cats!=Null) { ?>
									<i class="icon-th-list"></i>
									<?php foreach( $cats as $cat ){?>
									<a href="<?php echo get_term_link( $cat->term_id, 'product_cat' ); ?>" ><?php echo $cat->name;?></a>
									<?php } ?>
								<?php } ?>
						</div>
						<div class="item_image">
							<?php echo $thumb_image ?>
								<div class="item-opacity">
									<span class="product_zoom">
										<a class="group" id="product_<?php echo $post->ID; ?>" href="<?php echo $image[0]; ?>" title="<?php echo $post->post_title;?>">
										
										</a>
									</span>
								</div>
						</div>
						<div class="product_category_content">
								<div class="item_title">
									<a href="<?php echo $post->guid ?>"><?php echo $post->post_title;?></a>
								</div>
								<div class="item_price">
									<span>
										<?php echo $product->get_price_html(); ?>
									</span>
								</div>
								<div class="button btn-cart" title=" Add to cart "  >
									<i class="ico-cart"></i>
									<div class="add-to-cart">
										<?php echo apply_filters( 'woocommerce_loop_add_to_cart_link',
														sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="button %s product_type_%s">%s</a>',
															esc_url( $product->add_to_cart_url() ),
															esc_attr( $product->id ),
															esc_attr( $product->get_sku() ),
															$product->is_purchasable() ? 'add_to_cart_button' : '',
															esc_attr( $product->product_type ),
															esc_html( $product->add_to_cart_text() )
														),
													$product ); ?>
									</div>
								</div>
								<div class="item-bottom clearfix">
											<?php 
											$count = $wpdb->get_var("
												SELECT COUNT(meta_value) FROM $wpdb->commentmeta
												LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
												WHERE meta_key = 'rating'
												AND comment_post_ID = $post->ID
												AND comment_approved = '1'
												AND meta_value > 0
											");

											$rating = $wpdb->get_var("
												SELECT SUM(meta_value) FROM $wpdb->commentmeta
												LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
												WHERE meta_key = 'rating'
												AND comment_post_ID = $post->ID
												AND comment_approved = '1'
											");
											?>
											<div class="reviews-content">
												<?php
													if( $count > 0 ){
														$average = number_format($rating / $count, 1);
												?>
													<div class="star"><span style="width: <?php echo ($average*14).'px'; ?>"></span></div>
													
												<?php } else { ?>
												
													<div class="star"></div>
													
												<?php } ?>
													<div class="item-number-rating">
														<?php echo $count; _e(' vote(s)', 'smartaddons');?>
													</div>
											</div>
										</div>
								<!-- add to card -->
								
						</div>
					</div>
				</div>	
			</div>
		<?php endwhile; wp_reset_query(); ?>
		</div>
	</div>
</div>
<?php } }?>